﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SpringWaterDataAccess;
using Utilities;
using System.IO;

namespace sow
{
	/// <summary>
	/// Summary description for yuedu.
	/// </summary>
	public partial class yuedu : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label lblTitle;
		protected System.Web.UI.WebControls.Label lblAuthor;
		protected System.Web.UI.WebControls.HyperLink addComment;
		protected System.Web.UI.WebControls.HyperLink readComments;
		protected System.Web.UI.WebControls.LinkButton Linkbutton2;

		private DataRow wordRow = null;

		protected void Page_Load(object sender, System.EventArgs e)
		{
		      string wordID = Request.QueryString.Get("wordID");
			  UserObject.IncreaseNumberClicks(wordID);
		}

		private void getWordRow(string wordID)
		{
            if (wordID == null) return;
			// Lazy load
			if (wordRow == null) 
			{

				DataSet theWord = UserObject.getWordByID(wordID);
				DataTable wordTable = theWord.Tables[0];
				wordRow = wordTable.Rows[0];
			}
		}

		
		public string getURL(string wordID) 
		{
			getWordRow(wordID);
			string categoryID = System.Convert.ToString(wordRow["CategoryID"]);
			Response.Write(Path.GetExtension(wordRow["URL"] + ""));

			return "../../" + Helper.getLocationFromCategory(categoryID) + "/" + wordRow["URL"];
		}

		public void ReadOrPlayFile()
		{
            if (wordRow == null) return;

			string categoryID = System.Convert.ToString(wordRow["CategoryID"]);
			if (Path.GetExtension(wordRow["URL"] + "").Equals(".wmv")) 
			{
				string strDomain = Request.ServerVariables["HTTP_HOST"].ToString();
				
				Response.Redirect("http://" + strDomain + 
					Helper.getLocationFromCategory(categoryID) + "/" + wordRow["URL"]);
			} 
			else 
			{
				Server.Execute(getURL(Request.QueryString.Get("wordID")));
			}
			
		}

		public string getNumberOfVisits(string wordID)
		{
			getWordRow(wordID);
            if (wordRow != null)
            {
                return "" + wordRow["IncreaseNumberOfClicks"];
            }
            else
            {
                return "";
            }
		}

		public string getRecommendBy(string wordID)
		{
			getWordRow(wordID);
            if (wordRow != null)
            {
                return "" + wordRow["RecommendBy"];
            }
            else
            {
                return "";
            }
		 
		}

		public string getPartnerURL(string wordID)
		{
            if (wordRow == null) return "";
			getWordRow(wordID);
			int partnerID = System.Convert.ToInt32(wordRow["PartnerID"]);
			//DataSet partner = UserObject.getPartner(partnerID);
			
			DataRow partnerRow = UserObject.getPartnerRow(partnerID);

			
			return "<A href=" + System.Convert.ToString(partnerRow["URL"]) + ">" + 
		                UserObject.getPartner(partnerID) + "</A>";
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void LinkButton1_Click(object sender, System.EventArgs e)
		{
			HttpCookie emailCookie = Request.Cookies["email"];

			// Read the cookie information and display it.
			if (emailCookie == null) 
			{
				Response.Redirect("index.aspx?message=请您先登记/登录，再使用'我的书架'!");
			} 

			// If it is from yuedu.aspx, we need to place teh article 
			// into bookshelf
			if (Request.QueryString.Get("wordId") != null) 
			{
				UserObject.AddWordToShelf(emailCookie.Value,
											int.Parse(Request.QueryString.Get("wordId")));
			}

			Response.Redirect("shujia.aspx");
		}

		protected void LinkButton3_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("forumsStart.aspx?ForumID=12&ForumName=泉源评论&CategoryID=39&CategoryName=评论泉源网文章");
		}

		
	}
}
